﻿<?php 
class CurrencyController extends Controller{
	public function __construct(){
		$this->calledClass(get_class($this));
	}
	public function DisplayAdmin(){
		$currency = $this->loadModel("Currency");
		$query = $currency->queryJoinTable();
		$this->set(array("list"=>$currency->DisplayAdmin($query,$this->get("page"),5,array("name"=>$this->get("name"),"symbol"=>$this->get("symbol")),array("created"),"DESC","AND")));
		$this->set(array("urls"=>array("name"=>$this->get("name"),"symbol"=>$this->get("symbol"))));
		$this->set(array("getAllCurrency"=>$currency->GetAllCurrency()));
		$this->render("admin");
	}
	public function save(){
		$currency = $this->loadModel("Currency");
		$querycompare = $currency->findObject(
				array("name="=>$this->get("txtName"),
					  "symbol="=>$this->get("txtSymbol")));
		if($querycompare == NULL){
			$currency->save(array(
				"name"=>ucwords(trim($this->get("txtName"))),
				"symbol"=>trim($this->get("txtSymbol")),
				"version"=>1,
				"created"=>date("Y-m-d H:i:s")));
		}else{	
			if($querycompare->name == ucwords($this->get("txtName")) || $querycompare->symbol == trim($this->get("txtSymbol"))){
				?>
					<script type="text/javascript">
						alert("You input duplicate data");
					</script>
					<?php
				}else{
					$currency->save(array(
					"name"=>ucwords(trim($this->get("txtName"))),
					"symbol"=>trim($this->get("txtSymbol")),
					"version"=>1,
					"created"=>date("Y-m-d H:i:s")));
				}	
			}	
		$this->AddNew();
	}
	public function AddNew(){
		$this->set(array("layout"=>"addnew"));
		$this->render("admin");
	}
	public function GetField(){
		$currency = $this->loadModel("Currency");
		$checkedbox = $this->get("pid");
		if($checkedbox){
			$query = $currency->queryJoinTable();
			$getfield = $currency->GetFieldWithMultiTable($query,array("Currencys.id="=>$checkedbox));
			$this->set(array("getfield"=>$getfield));
			$this->set(array("layout"=>"edit"));
			$this->render("admin");
		}else {
			$this->DisplayAdmin();
		}
	}
	public function delete(){
		$currency = $this->loadModel("Currency");
		$checkedbox = $this->get("id");
		if($checkedbox){
			$currency->delete(array($checkedbox),true);
			$this->DisplayAdmin();
		}else {
			$this->DisplayAdmin();
		}
	}
	public function SaveEdit(){
		$currency = $this->loadModel("Currency");
		$checkedbox = $this->get("checkbox");
		if($checkedbox){
			$data = $currency->version("Currencys","version",array("id="=>$checkedbox));
			$version = $data + 1;
			$currency->edit(array(
							"name"=>ucwords(trim($this->get("txtName"))),
							"symbol"=>ucwords(trim($this->get("txtSymbol"))),
							"version"=>$version,
							"modify"=>date("Y-m-d H:i:s")),array("id="=>$checkedbox));
			$this->DisplayAdmin();
		}else{
			$this->DisplayAdmin();
		} 

	}
}
?>